package com.dmx.fw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.dmx.fw.bean.mapping.pojo.Operator;
import com.dmx.fw.bean.mapping.pojo.ParameterData;
import com.dmx.fw.bean.mapping.pojo.Role;
import com.dmx.fw.dao.interfaces.OperatorDao;
import com.dmx.fw.service.interfaces.OperatorService;
import com.dmx.fw.bean.MenuBean;
import com.dmx.fw.dao.interfaces.JdbcDao;

public class OperatorServiceImpl extends BaseServiceImpl implements
		OperatorService {

	private OperatorDao operatorDao;

	private JdbcDao jdbcDao;

	public JdbcDao getJdbcDao() {
		return jdbcDao;
	}

	/**
	 * @param jdbcDao
	 *            the jdbcDao to set
	 */
	public void setJdbcDao(JdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

	public Operator getOperator(String loginName) {
		List temp = operatorDao.findByLoginName(loginName);
		Operator operator = null;
		if (temp != null && !temp.isEmpty()) {
			operator = (Operator) temp.get(0);
			if (operator.getStatus() == null || !operator.getStatus().equals("1")) {
				return null;
			}
			StringBuffer sqlBuffer=new StringBuffer(100);
			sqlBuffer.append("select t.role_id from SYSTEM_OPTR_ROLE t where t.optr_id=").append(operator.getId()); 
			List queryList = this.jdbcDao.queryData(sqlBuffer.toString());
			operator.getRoleIds().clear();
			for (int i = 0; i < queryList.size(); i++) {
				List valList = (List) queryList.get(i);
				operator.getRoleIds().add(valList.get(0).toString());
			}
		}
		return operator;
	}

	public List getRoles(Operator operator) {
		// TODO Auto-generated method stub
		return null;
	}

	public void removeOperator(Long operatorId) {
		// TODO Auto-generated method stub

	}

	public void saveOperator(Operator operator) throws Exception{
		operatorDao.saveObject(operator);
	}
	
	
	public Map getAuthFuncByOptrId(Long operatorId){
		StringBuffer sqlBuffer = new StringBuffer("select distinct a.FUNC_ID,a.ACSS_URL ")
		.append(" from func a,role_func_rel b, optr_role_rel c where")
		.append(" a.status='valid' and a.IS_MENU=0 and a.func_id=b.func_fk and b.role_fk = c.role_fk")
		.append(" and c.optr_fk="+operatorId.intValue());
		List list = jdbcDao.queryData(sqlBuffer.toString());
		Map returnMap = new HashMap();
		if (list!=null){
			for(int i = 0;i<list.size();i++){
				List valList = (List) list.get(i);
				returnMap.put((String)valList.get(1),(String)valList.get(0));
			}	
		}
		return returnMap;
	}

	public List getFuncsByOptrId(Long operatorId) {
		// 此处模拟从数据库获得菜单信息

		StringBuffer sqlBuffer = new StringBuffer(" select bb.func_id,")
				// 0
				.append(" bb.par_func_id,")
				// 1
				.append(" level,")
				// 2
				.append(" bb.ACSS_URL,")
				// 3
				.append(" bb.name,")
				// 4
				.append(" bb.checked,")
				// 5
				.append(" bb.IS_SHORTCUT,")
				// 6
				.append(" bb.NORMAL_ICON,")
				// 7
				.append(" bb.DIS_ICON_NAME,")
				// 8
				.append(" bb.MOUSEOVER_ICON_NAME,")
				// 9
				.append(" bb.MENU_SORT,")
				// 10
				.append(" bb.SHORTCUT_SORT")
				// 11
				.append(" from (select distinct aa.func_id,")
				.append(" aa.par_func_id,")
				.append(" aa.ACSS_URL,")
				.append(" aa.name,")
				.append(" aa.checked,")
				.append(" aa.IS_SHORTCUT,")
				.append(" aa.NORMAL_ICON,")
				.append(" aa.DIS_ICON_NAME,")
				.append(" aa.MOUSEOVER_ICON_NAME,")
				.append(" aa.MENU_SORT,")
				.append(" aa.SHORTCUT_SORT")
				.append(" from func aa where aa.status='valid' and aa.IS_MENU=1")
				.append(" connect by prior aa.par_func_id=aa.func_id")
				.append(" start with aa.func_id in (select b.func_fk")
				.append(" from role_func_rel b, optr_role_rel c")
				.append(" where b.role_fk = c.role_fk")
				.append(" and c.optr_fk = " + operatorId.intValue() + ")")
				.append(") bb connect by prior bb.func_id=bb.par_func_id")
				.append(
						" start with (bb.par_func_id is null or bb.par_func_id=0) order by level,MENU_SORT");
		

		List queryList = jdbcDao.queryData(sqlBuffer.toString());
		List menuList = new ArrayList();

		for (int i = 0; i < queryList.size(); i++) {
			List valList = (List) queryList.get(i);
			MenuBean menuBean = new MenuBean();
			menuBean.setMenuPk((String) valList.get(0));
			if ("0".equals((String) valList.get(1))) {
				menuBean.setParentMenuId("");
			} else {
				menuBean.setParentMenuId((String) valList.get(1));
			}
			menuBean.setActived("Y");
			menuBean.setLevel(Integer.parseInt((String) valList.get(2)));
			menuBean.setUrl((String) valList.get(3));
			menuBean.setDescription((String) valList.get(4));
			if ("1".equals((String) valList.get(5))) {
				menuBean.setChecked("Y");
			} else {
				menuBean.setChecked("N");
			}
			if ("1".equals((String) valList.get(6))) {
				menuBean.setIsShortcut("Y");
			} else {
				menuBean.setIsShortcut("N");
			}
			menuBean.setIconName((String) valList.get(7));
			menuBean.setDisIconName((String) valList.get(8));
			menuBean.setMouseOverIconName((String) valList.get(9));
			try {
				menuBean.setSort(Integer.parseInt((String) valList.get(10)));
			} catch (Exception e) {
				menuBean.setSort(0);
			}

			try {
				menuBean.setShortCutSort(Integer.parseInt((String) valList
						.get(11)));
			} catch (Exception e) {
				menuBean.setShortCutSort(0);
			}

			menuList.add(menuBean);

		}
		return menuList;
	}

	public List getRolesById(Long operatorId) {
		return operatorDao.getRolesById(operatorId);
	}

	public List getOperatorType() {
		return operatorDao.getOpratorTypes();
	}

	/**
	 * @return the operatorDao
	 */
	public OperatorDao getOperatorDao() {
		return operatorDao;
	}

	/**
	 * @param operatorDao
	 *            the operatorDao to set
	 */
	public void setOperatorDao(OperatorDao operatorDao) {
		this.operatorDao = operatorDao;
	}

	public Operator getOperator(Long optrId) {
		Operator operator = (Operator) operatorDao.getObject(Operator.class,
				optrId);
		return operator;
	}

	public void save(Operator operator, String[] roleArray) {
		Set roles = new HashSet();
		if (roleArray != null && roleArray.length > 0) {
			for (int i = 0; i < roleArray.length; i++) {
				Role role = (Role) operatorDao.getObject(Role.class, Long
						.parseLong(roleArray[i]));
				roles.add(role);
			}
			operator.setRoles(roles);
		}
		operatorDao.saveOrUpdate(operator);

	}

	public void update(Operator operator, String[] roleArray) {
		Operator db = (Operator) operatorDao.getObject(Operator.class, operator
				.getId());
		db.setRoles(null);

		if (roleArray != null && roleArray.length > 0) {
			Set roles = new HashSet();
			for (int i = 0; i < roleArray.length; i++) {
				Role role = (Role) operatorDao.getObject(Role.class, Long
						.parseLong(roleArray[i]));
				roles.add(role);
			}
			db.setRoles(roles);
		}
		//db.setMgrDmnCode(operator.getMgrDmnCode());
		db.setLogin(operator.getLogin());
		db.setName(operator.getName());
		if(operator.getPassword()!=null&&operator.getPassword().length()>0)
			db.setPassword(operator.getPassword());
		db.setPosition(operator.getPosition());
		db.setOfficePhone(operator.getOfficePhone());
		db.setOperatorType(operator.getOperatorType());
		operatorDao.saveOrUpdate(db);
	}

	public boolean checkLoginNameUnique(String login) {
		boolean flag = true;
		List temp = operatorDao.findByLoginName(login);
		if (temp == null || temp.isEmpty()) {
			flag = false;
		}
		return flag;
	}

	public void removeOperator(Operator operator) {
		/*Operator db = (Operator) operatorDao.getObject(Operator.class, operator.getId());
		db.setStatus("0");
		db.setRoles(null);
		operatorDao.saveOrUpdate(db);*/
		operatorDao.removeObject(operator);
	}

	public void changePsw(Operator operator) {
		operatorDao.saveOrUpdate(operator);

	}

	public List getAllRolesArray() {
		return operatorDao.getAllRoles();
	}

	public String getOperatorTypeName(String code) {
		ParameterData pd = operatorDao.getParameterDataByCode(code);
		return pd==null?"":pd.getName();
	}
}
